<?php

namespace app\beautiful\model\order;

use app\beautiful\model\goods\GoodsOtherSkuDetailModel;
use app\common\model\CommonModel;

use app\beautiful\model\mall\GoodsModel;
use app\stores\model\order\ChargebackModel;

class GoodsOrderItemModel extends CommonModel{

    protected  $name = 'beautiful_order_item';

    protected  $pk   = 'order_item_id';



    public function goods()

    {

        return $this->hasOne(GoodsModel::class,'goods_id','goods_id');

    }
    public function chargeback()

    {

        return $this->hasMany(ChargebackModel::class,'order_item_id','order_item_id');

    }
    public function order()

    {

        return $this->hasOne(GoodsOrderModel::class,'order_id','order_id');

    }
    public static function list($where,$order,$field='*'){
        $list = self::alias('oi')
            ->join('beautiful_order o','o.order_id=oi.order_id')
            ->join('beautiful_member m','m.member_id=o.member_id')
            ->leftjoin('beautiful_supplier_goods sg','sg.goods_id=oi.goods_id')
            ->leftjoin('beautiful_supplier s','s.id=sg.supplier_id')
            ->group('oi.order_item_id')
            ->order($order)
            ->field($field)
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['oi.shop_id'=>$where['shop_id']]);
            })->when(!empty($where['order_ids']),function ($query) use ($where){
                $query->whereIn('oi.order_id',$where['order_ids']);
            })->select()->toArray();
        foreach ($list as $k=>$v){
            if(isset($v['sg_is_delete']) && $v['sg_is_delete']==1){
                $list[$k]['supplier_name'] = '';
            }
            //单位
            $goods = GoodsModel::with(['unit'])->where(['goods_id'=>$v['goods_id']])->find();
            if($goods && isset($goods['unit']) && $goods['unit']){
                $unit = $goods['unit']['group_detail_name'];
            }
            $list[$k]['unit'] = isset($unit)?$unit:'无';
            $list[$k]['add_time_format'] = date('Y-m-d H:i:s',$v['add_time']);

        }
        return $list;
    }

}



